﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HousingDistribution
{
    class UpdateServiceLength
    {
        public UpdateServiceLength(DateTime date)
        {
            this.date = date;
        }

        public void Proc()
        {
            try
            {
                //工人、职员、士官都需要根据分房截止时间来更新工龄:1, 已退休，则根据退休时间计算；2，未退休，则根据分房截止时间计算
                //工龄要求满一年计算，即需要考虑月份
                //截止时间月份>工作时间月份，满年的

                StringBuilder updateWorker = new StringBuilder("update person inner join personsortinfo on person.idperson = personsortinfo.idperson set person.serviceLength = year('");
                updateWorker.Append(date.ToShortDateString());
                updateWorker.Append("') - year(personsortinfo.sortWorkTime) ");
                updateWorker.Append(" where person.type='");
                updateWorker.Append((int)PersonTypeEnum.Worker);
                updateWorker.Append("' and person.retirementTime ='");
                updateWorker.Append(DateProc.InvalidDate().ToShortDateString());
                updateWorker.Append("'");
                updateWorker.Append(" and month('");
                updateWorker.Append(date.ToShortDateString());
                updateWorker.Append("') >= ");
                updateWorker.Append(" month(personsortinfo.sortWorkTime) ");

                //截止时间月份<工作时间月份，不满年的
                StringBuilder updateWorker2 = new StringBuilder("update person inner join personsortinfo on person.idperson = personsortinfo.idperson set person.serviceLength = year('");
                updateWorker2.Append(date.ToShortDateString());
                updateWorker2.Append("') - year(personsortinfo.sortWorkTime) -1 ");
                updateWorker2.Append(" where person.type='");
                updateWorker2.Append((int)PersonTypeEnum.Worker);
                updateWorker2.Append("' and person.retirementTime ='");
                updateWorker2.Append(DateProc.InvalidDate().ToShortDateString());
                updateWorker2.Append("'");
                updateWorker2.Append(" and month('");
                updateWorker2.Append(date.ToShortDateString());
                updateWorker2.Append("') < ");
                updateWorker2.Append(" month(personsortinfo.sortWorkTime) ");

                DBProc.Instance().Excute(updateWorker.ToString());
                DBProc.Instance().Excute(updateWorker2.ToString());

                //职员
                StringBuilder updateStaff = new StringBuilder("update person inner join personsortinfo on person.idperson = personsortinfo.idperson set person.serviceLength = year('");
                updateStaff.Append(date.ToShortDateString());
                updateStaff.Append("') - year(personsortinfo.sortWorkTime) ");
                updateStaff.Append("where person.type='");
                updateStaff.Append((int)PersonTypeEnum.Staff);
                updateStaff.Append("' and person.retirementTime ='");
                updateStaff.Append(DateProc.InvalidDate().ToShortDateString());
                updateStaff.Append("'");
                updateStaff.Append(" and month('");
                updateStaff.Append(date.ToShortDateString());
                updateStaff.Append("') >= ");
                updateStaff.Append(" month(personsortinfo.sortWorkTime) ");

                StringBuilder updateStaff2 = new StringBuilder("update person inner join personsortinfo on person.idperson = personsortinfo.idperson set person.serviceLength = year('");
                updateStaff2.Append(date.ToShortDateString());
                updateStaff2.Append("') - year(personsortinfo.sortWorkTime) -1 ");
                updateStaff2.Append(" where person.type='");
                updateStaff2.Append((int)PersonTypeEnum.Staff);
                updateStaff2.Append("' and person.retirementTime ='");
                updateStaff2.Append(DateProc.InvalidDate().ToShortDateString());
                updateStaff2.Append("'");
                updateStaff2.Append(" and month('");
                updateStaff2.Append(date.ToShortDateString());
                updateStaff2.Append("') < ");
                updateStaff2.Append(" month(personsortinfo.sortWorkTime) ");

                DBProc.Instance().Excute(updateStaff.ToString());
                DBProc.Instance().Excute(updateStaff2.ToString());

                //士官
                StringBuilder updateNCO = new StringBuilder("update person inner join personsortinfo on person.idperson = personsortinfo.idperson set person.serviceLength = year('");
                updateNCO.Append(date.ToShortDateString());
                updateNCO.Append("') - year(personsortinfo.sortWorkTime) ");
                updateNCO.Append("where person.type='");
                updateNCO.Append((int)PersonTypeEnum.NCO);
                updateNCO.Append("' and person.retirementTime ='");
                updateNCO.Append(DateProc.InvalidDate().ToShortDateString());
                updateNCO.Append("'");
                updateNCO.Append(" and month('");
                updateNCO.Append(date.ToShortDateString());
                updateNCO.Append("') >= ");
                updateNCO.Append(" month(personsortinfo.sortWorkTime) ");

                StringBuilder updateNCO2 = new StringBuilder("update person inner join personsortinfo on person.idperson = personsortinfo.idperson set person.serviceLength = year('");
                updateNCO2.Append(date.ToShortDateString());
                updateNCO2.Append("') - year(personsortinfo.sortWorkTime) -1 ");
                updateNCO2.Append(" where person.type='");
                updateNCO2.Append((int)PersonTypeEnum.NCO);
                updateNCO2.Append("'");
                updateNCO2.Append(" and month('");
                updateNCO2.Append(date.ToShortDateString());
                updateNCO2.Append("') < ");
                updateNCO2.Append(" month(personsortinfo.sortWorkTime) ");

                DBProc.Instance().Excute(updateNCO.ToString());
                DBProc.Instance().Excute(updateNCO2.ToString());
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }
        }

        private DateTime date;
    }
}
